package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjCustomer;
import com.pmcc.cj.entity.CjRobotAttest;
import com.pmcc.cj.repository.CjCustomerRepository;
import com.pmcc.cj.repository.CjRobotAttestRepository;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tomas on 2020/10/11.
 */
@Transactional
@Service
public class CjRobotAttestService {
    @Autowired
    CjRobotAttestRepository cjRobotAttestRepository;
    @Autowired
    CjCustomerRepository cjCustomerRepository;
    @Value("${web.downloadFile}")
    private  String downloadFile;

    public ResultBean robotApprove(CjRobotAttest model){
        ResultBean resultBean =new ResultBean(ResultBean.SUCCESS);
        try {
            model.setCreateDate(new Date());
            model.setFettle(0);
            model.setCustomerId(OnlineUser.getEmployeeId());
            model.setRobotFlag(0);//默认推送
            cjRobotAttestRepository.saveAndFlush(model);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean queryRobotAttestByPage(CjRobotAttest model, AntdPage antdPage){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjRobotAttestRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<CjRobotAttest> getSpecification(CjRobotAttest model) {
        return new Specification<CjRobotAttest>() {
            @Override
            public Predicate toPredicate(Root<CjRobotAttest> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtil.isNotEmpty(model.getName())){
                    list.add(builder.like(root.get("name").as(String.class),  "%"+model.getName().trim()+"%"));
                }
                if(model.getFettle() != null){
                    list.add(builder.equal(root.get("fettle").as(Integer.class),  model.getFettle()));
                }
                query.orderBy(builder.desc(root.get("createDate").as(Date.class)));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    public ResultBean verificationRobot(String id,String type){
        ResultBean resultBean =new ResultBean(ResultBean.SUCCESS);
        try {
            if("1".equals(type)){
                //审核通过
                CjRobotAttest robotAttest = cjRobotAttestRepository.findOne(id);
                robotAttest.setFettle(1);
                robotAttest.setVerifyTime(new Date());
                Boolean isTrue=QRcodeImageUtil.buildQuickMark(robotAttest.getCustomerId(),downloadFile,robotAttest.getCustomerId());
                if(isTrue){
                    robotAttest.setQrCode("/gek/"+robotAttest.getCustomerId()+".png");//保存二维码路径
                }
                cjRobotAttestRepository.saveAndFlush(robotAttest);
                CjCustomer customer = cjCustomerRepository.findOne(robotAttest.getCustomerId());
                customer.setDriverAuth(true);
                customer.setIntegral(1000.0);
                customer.setCardName(robotAttest.getName());
                cjCustomerRepository.saveAndFlush(customer);
            }else if("2".equals(type)){
                //审核拒绝
                CjRobotAttest robotAttest = cjRobotAttestRepository.findOne(id);
                robotAttest.setFettle(2);
                robotAttest.setVerifyTime(new Date());
                cjRobotAttestRepository.saveAndFlush(robotAttest);
            }
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean getRobot(){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjRobotAttest cjRobotAttest=cjRobotAttestRepository.findByCustomerId(OnlineUser.getEmployeeId());
            if(cjRobotAttest !=null){
                resultBean.setObject(cjRobotAttest);
            }else{
                resultBean.setResultCode(ResultBean.FAIL);
                resultBean.setResultDesc("无认证");
            }
        }catch (Exception e ){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean robotFlagChange(String robotFlag){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjCustomer customer = cjCustomerRepository.findOne(OnlineUser.getEmployeeId());
            CjRobotAttest robotAttest = cjRobotAttestRepository.findByCustomerId(OnlineUser.getEmployeeId());
            customer.setRobotFlag(Integer.parseInt(robotFlag));
            robotAttest.setRobotFlag(Integer.parseInt(robotFlag));
            cjCustomerRepository.saveAndFlush(customer);
            cjRobotAttestRepository.saveAndFlush(robotAttest);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
